package com.teatrace.action.produce;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import com.teatrace.model.base.Device;
import com.teatrace.model.base.Employee;
import com.teatrace.service.produce.ProduceService;
import com.teatrace.service.produce.impl.ProduceServiceImpl;

public class ActionUtils {

	private static final Logger log = Logger.getLogger(ActionUtils.class);
	
	private static ActionUtils actionUtils = null;
	
	private ProduceService produceService = null;
	
	private ActionUtils(){
		produceService = new ProduceServiceImpl();
	}
	public static ActionUtils getInstance(){
		
		if(actionUtils == null){
			actionUtils = new ActionUtils();
		}
		return actionUtils;
	}
	
	/**
	 * 处理从页面传过来得 员工编号字符串  
	 * @param emps
	 * @return  员工集合
	 */
	public Set<Employee> getEmployees(String emps){
		log.debug("查出员工集合");
		Set<Employee> employeeSet = new HashSet<Employee>();
		Employee employee = null;
		if(emps.contains(",")){
			String[] employees = emps.split(",");
			for(int i = 0; i<employees.length;i++){
				try {
					employee = (Employee) produceService.loadById(Employee.class,Integer.parseInt(employees[i].trim()));
					
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				employeeSet.add(employee);
			}
		}else{
			try {
				employee = (Employee) produceService.loadById(Employee.class,Integer.parseInt(emps.trim()));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			employeeSet.add(employee);
		}
		return employeeSet;
	}
	
	
	/**
	 * 处理从页面传过来
	 * @param devices 字符串  
	 * @return
	 * @throws Exception
	 */
	public Set<Device> getDevices(String devices) {
		Set<Device> deviceSet = new HashSet<Device>();
		Device device = null;
		if(devices.contains(",")){
			String[] devicesArray = devices.split(",");
			for(int i = 0; i<devicesArray.length;i++){
				
				try {
					device = (Device) produceService.loadById(Device.class,Integer.parseInt(devicesArray[i].trim()));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				deviceSet.add(device);
			}
		}else{
			try {
				device = (Device) produceService.loadById(Device.class, Integer.parseInt(devices.trim()));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			deviceSet.add(device);
		}
		return deviceSet;
	}
	
}
